package com.dup.jxlh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.auth.AuthInfo;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.entities.vo.AdminVO;
import com.dup.jxlh.admin.mapper.AdminMapper;
import com.dup.jxlh.admin.service.AdminService;
import com.dup.jxlh.admin.service.OwnValidatorService;
import com.dup.jxlh.admin.service.auth.AuthService;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.exception.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;

@Service("adminServiceImpl")
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Autowired
    private AuthService authService;

    @Autowired
    private OwnValidatorService ownValidatorService;

    @Override
    public AdminVO getAdminInfo(BigInteger id) {
        return adminMapper.getAdminInfo(id);
    }

    @Override
    @CacheEvict(value = "admin", key = "#admin.id")
    public Boolean updateAdminInfo(Admin admin) {
        if (admin.getPhone() == null) {
            throw new AppException(ResultEnum.USER_FOUND_EXIST);
        }
        if (adminMapper.checkAdminByIdAndPhone(admin.getId(), admin.getPhone()) != null) {
            throw new AppException(ResultEnum.USER_FOUND_EXIST);
        }
        if (adminMapper.updateById(admin) == 1) {
            return authService.createCurrentAuthPermFlag(admin.getPhone());
        }
        return false;
    }

    @Override
    public Boolean updateAdminPassword(BigInteger id, String password) {
        return adminMapper.updateAdminPassword(id, password);
    }

    @Override
    @Cacheable(value = "admin", key = "#id")
    public AuthInfo getAuthInfo(BigInteger id) {
        return adminMapper.getAuthInfo(id);
    }

    @Override
    public Page<AdminVO> getAdminList(PaginationData paginationData, Admin admin) {
        Page<AdminVO> adminVOPage = new Page<>(paginationData.getCurrent(), paginationData.getSize());
        adminVOPage.setOptimizeCountSql(false);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        ownValidatorService.setQueryWrapperByCompany(queryWrapper, paginationData);

        if (!paginationData.getSearch().isEmpty()) {
            queryWrapper.and(i -> i
                    .like("phone", paginationData.getSearch())
                    .or()
                    .like("realname", paginationData.getSearch()));
        }

        if (admin.getDepartmentId() != null) {
            queryWrapper.eq("department_id", admin.getDepartmentId());
        }

        if (admin.getRoleId() != null) {
            queryWrapper.eq("role_id", admin.getRoleId());
        }

        if (admin.getLevel() != null) {
            queryWrapper.eq("level", admin.getLevel());
        }

        if (admin.getEnable() != null) {
            queryWrapper.eq("is_enable", admin.getEnable());
        }

        ArrayList<AdminVO> adminList = adminMapper.getAdminList(adminVOPage, queryWrapper);
        adminVOPage.setRecords(adminList);
        return adminVOPage;
    }

    @Override
    @Transactional
    public Boolean addAdminInfo(Admin admin) {
        if (adminMapper.insert(admin) == 1) {
            return updateAdminPassword(admin.getId(), "$2a$10$mQtfljbyADgqRJNJep9ZvecUQrTqadEMgOLpQ8ncL.HG5Qzl.ZvJK");
        }
        return false;
    }

}
